//
//  CycJson.m
//  Cycling
//
//  Created by cjg.mini on 2023/3/3.
//

#import "CycJson.h"

@implementation NSDictionary (CycJson)

- (NSString *)Cyc_JSONString {
    if ([NSJSONSerialization isValidJSONObject:self]) {
        NSError *error;
        NSData *registerData = [NSJSONSerialization dataWithJSONObject:self options:kNilOptions error:&error];
        return [[NSString alloc] initWithData:registerData encoding:NSUTF8StringEncoding];
    }
    return nil;
}

@end

@implementation NSArray (CycJson)

- (NSString *)Cyc_JSONString {
    if ([NSJSONSerialization isValidJSONObject:self]) {
        NSError *error;
        NSData *registerData = [NSJSONSerialization dataWithJSONObject:self options:kNilOptions error:&error];
        return [[NSString alloc] initWithData:registerData encoding:NSUTF8StringEncoding];
    }
    return nil;
}

@end

@implementation NSString (CycJson)

- (id)Cyc_objectFromJSONString {
    NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error;
    id result = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    if (error) {
        NSLog(@"%@", error.description);
    }
    return result;
}

@end
